import {
    sampleSize, //获得随机数
    uniq, //去重
    countBy,
    take, //从0开始获得指定数组
    takeRight,
    slice,
    indexOf,
    drop,
    dropRight//裁剪数组
} from "lodash"
import { ArrayHas, getEvenCount, getOddAndEven } from "@/utils/format";
window.countBy = countBy;
/** 
 * 对所有结果进行过滤
*/
function getFilterResult({
    allRedBallList,
    allRedBallStrList,
    historyList,
    historyResultStrList,
    historyOddEvenList
}:any){
    
  // console.clear()
  let returnRes:any ={
    result:[],//所有可能的结果
    nearByList: [],//去掉上一期上下浮动20万条数据
    continue4:[],//4/5/6连号
    continue3:[],//3连号
    oneMore456:[],//1区大于4 5 6个数
    twoMore456:[],//2区大于4 5 6个数
    threeMore456:[],//3区大于4 5 6个数
    evenCount6: [],//偶数6
    evenCount5: [],//偶数5
    oddCount6: [],//奇数6
    oddCount5: [],//奇数5
    drop1112And2223: [],//去掉对称数（比如01&02和32&33）
    red2BigRed1Nine:[],//红球2-红球1大于9
    red654321jinge:[],//红6-红5 && 红4-红3 && 红2-红1 的结果同时=1 2 3 4 5 6 7 8 9 10 11时
    red3164dengyu10:[],//红3-红1 = 10 && 统计红6-红4 = 10
    tongwei:[],//去掉出现3同尾，或者4个1同尾的情况
    dropTopOddAndEevn: [],//去掉最近20期的奇偶
    top20Red21Red32Red43: [],//去掉最近20期，红2-红1，红3-红2，红4-红3的相同间隔的记录
    red6red1:[],//去掉红6-红1小于等于13
    filterResult:[],//过滤后的号码
    removeList:[],//移除的记录
  }
  returnRes.result = allRedBallList;
  
  returnRes.historyListTop20 = take(historyResultStrList, 20)//最近20条开奖记录
  let top20Red21Red32Red43:string[] = [];//记录一次最近20期，红2-红1，红3-红2，红4-红3的间隔，从总记录里面去掉这些记录
  let topHist = take(historyResultStrList, 500);
  topHist.forEach((redStr)=>{
    let redBall = historyList[redStr].number;
    let number1 = parseInt(redBall[0]);
    let number2 = parseInt(redBall[1]);
    let number3 = parseInt(redBall[2]);
    let number4 = parseInt(redBall[3]);
    let number5 = parseInt(redBall[4]);
    let number6 = parseInt(redBall[5]);
    top20Red21Red32Red43.push("" + (number2 - number1) + (number3 - number2) + (number4 - number3) + (number5 - number4)+ (number6 - number5));
  })
  
  
  let filterResult:any = [];//筛选结果，默认先复制所有计算结果

  //1.去掉上一期上下浮动20万条数据
  let lastRedBallStr = historyResultStrList[0];//最新一期红球
  let sliceIndex = allRedBallStrList.indexOf(lastRedBallStr);//最近一期的索引

  let fanwei = 100000;//上下浮动10万
  let leftFilter = take(allRedBallList, sliceIndex - fanwei);//选择左边的范围
  let rightFilter = takeRight(allRedBallList, allRedBallList.length - sliceIndex - fanwei);//选择右边的范围
  filterResult = leftFilter.concat(rightFilter);//保留选择的左右范围
  returnRes.nearByList = slice(allRedBallList, sliceIndex - fanwei, sliceIndex + fanwei);//记录被去掉的数据 

  filterResult = filterResult.filter(function(item:any){
    //2.去掉4连号和3连号
    let number1:any = parseInt(item[0]);
    let number2:any = parseInt(item[1]) - 1;
    let number3:any = parseInt(item[2]) - 2;
    let number4:any = parseInt(item[3]) - 3;
    let number5:any = parseInt(item[4]) - 4;
    let number6:any = parseInt(item[5]) - 5;
    let numbersObj = countBy([number1, number2, number3, number4, number5, number6])
    let numbers:number[] = [];
    Object.keys(numbersObj).forEach((key)=>{
        numbers.push(numbersObj[key]);
    });
    
    //去掉4连号
    if(numbers.indexOf(4) > -1 || numbers.indexOf(5) > -1 || numbers.indexOf(6) > -1){
        // console.log('4连号', item);
        returnRes.continue4.push(item);
        return false;
    }

    //3.去掉3连号
    if(numbers.indexOf(3) > -1){
        // console.log('3连号', item);
        returnRes.continue3.push(item);
        return false;
    }

    return true;
  });
  
  filterResult = filterResult.filter(function(item){
    let number1 = parseInt(item[0]);
    let number2 = parseInt(item[1]);
    let number3 = parseInt(item[2]);
    let number4 = parseInt(item[3]);
    let number5 = parseInt(item[4]);
    let number6 = parseInt(item[5]);

    //4. 去掉1区有 4 5 6个数的情况 1~11
    if(number4 <= 11 || number5 <= 11 || number6 <= 11){
        returnRes.oneMore456.push(item);
        //第4个数字小于等于11
        return false;
    }

    //5. 2区在4个内的个数 12~22
        //去掉2区包含4个数
    if(number1 >=12 && number4 <=22){
        /*0 4 2 */
        returnRes.twoMore456.push(item);
        return false
    }
    if(number2 >=12 && number5 <=22){
        returnRes.twoMore456.push(item);
        /*1 4 1 */
        return false
    }
    if(number3 >=12 && number6 <=22){
        returnRes.twoMore456.push(item);
        /*2 4 0 */
        return false
    }

    //去掉2区包含5个数
    if(number1 >= 12 && number5 <= 22){
        returnRes.twoMore456.push(item);
        /*0 5 1 */
        return false
    }
    if(number2 >= 12 && number6 <= 22){
        returnRes.twoMore456.push(item);
        /*1 5 0 */
        return false
    }

    //去掉2区包含6个数
    if(number1 >= 12 && number6 <= 22){
        returnRes.twoMore456.push(item);
        /*0 6 0 */
        return false
    }

    //6. 去掉3区包含4 5 6个数 23~33
    if(number1 >= 23/*3区包含6个数*/ || number2 >= 23/*3区包含5个数*/ || number3 >= 23 /*3区包含4个数*/){
        returnRes.threeMore456.push(item);
        return false;
    }

    //7. 去掉奇数和偶数
    //偶数数量
    let evenNum = getEvenCount(item);
    //奇数数量
    let oddNum = 6 - evenNum;

    //去掉偶数6个
    if(evenNum == 6){
        returnRes.evenCount6.push(item);
        return false;
    }

    //去掉偶数5个
    if(evenNum == 5){
        returnRes.evenCount5.push(item);
        return false;
    }

    //去掉6奇数
    if(oddNum == 6){
        returnRes.oddCount6.push(item);
        return false;
    }
    //去掉5奇数
    if(oddNum == 5){
        returnRes.oddCount5.push(item);
        return false;
    }

    //8. 去掉对称数 （比如01&02和32&33）
    if(ArrayHas(item, ["01", "02", "32", "33"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["02", "03", "31", "32"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["03", "04", "30", "31"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["04", "05", "29", "30"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["05", "06", "28", "29"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["06", "07", "27", "28"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["07", "08", "26", "27"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["08", "09", "25", "26"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["09", "10", "24", "25"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["10", "11", "23", "24"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }
    if(ArrayHas(item, ["11", "12", "22", "23"])){
        returnRes.drop1112And2223.push(item);
        return false;
    }

    //去掉红球1-红球2 > 9的情况
    if(number2 - number1 > 9){
        returnRes.red2BigRed1Nine.push(item);
        return false;
    }

    //红6-红5 && 红4-红3 && 红2-红1 的结果同时=1 2 3 4 5 6 7 8 9 10 11时
    let n1=1,n2=2,n3=3,n4=4,n5=5,n6=6,n7=7,n8=8,n9=9,n10=10,n11=11;
    if(
        number6 - number5 == n1 && number4 - number3 == n1 && number2 - number1 == n1 ||
        number6 - number5 == n2 && number4 - number3 == n2 && number2 - number1 == n2 ||
        number6 - number5 == n3 && number4 - number3 == n3 && number2 - number1 == n3 ||
        number6 - number5 == n4 && number4 - number3 == n4 && number2 - number1 == n4 ||
        number6 - number5 == n5 && number4 - number3 == n5 && number2 - number1 == n5 ||
        number6 - number5 == n6 && number4 - number3 == n6 && number2 - number1 == n6 ||
        number6 - number5 == n7 && number4 - number3 == n7 && number2 - number1 == n7 ||
        number6 - number5 == n8 && number4 - number3 == n8 && number2 - number1 == n8 ||
        number6 - number5 == n9 && number4 - number3 == n9 && number2 - number1 == n9 ||
        number6 - number5 == n10 && number4 - number3 == n10 && number2 - number1 == n10 ||
        number6 - number5 == n11 && number4 - number3 == n11 && number2 - number1 == n11
    ){
        returnRes.red654321jinge.push(item);
        return false;
    }

    //红3-红1 = 10 && 红6-红4 = 10
    if(number3 - number1 == 10 && number6 - number4 == 10){
        returnRes.red3164dengyu10.push(item);
        return false;
    }

    //去掉出现3同尾，或者4个1同尾的情况
    var pp:any[] = [];
    pp.push(number1%10);
    pp.push(number2%10);
    pp.push(number3%10);
    pp.push(number4%10);
    pp.push(number5%10);
    pp.push(number6%10);
    pp = countBy(pp);
    
    let twlen = Object.keys(pp).length;
    if(twlen === 6){
        //ppp.push("0同尾")
    }else{
        let needRemove =  false;
        Object.keys(pp).forEach((key:any)=>{
            if(pp[key] == 4){//这种情况是出现了4个同尾号
                needRemove = true;
            }
            if(pp[key] == 3){//这种情况是出现了3个同尾号
                needRemove = true;
            }
        });
        if(needRemove){
            returnRes.tongwei.push(item);
            return false;
        }
    }

    //top20Red21Red32Red43: [],//去掉最近20期，红2-红1，红3-红2，红4-红3的相同间隔的记录
    let compareStr = "" + (number2 - number1) + (number3 - number2) + (number4 - number3) + (number5 - number4)+ (number6 - number5);
    if(ArrayHas(top20Red21Red32Red43, compareStr)){
        returnRes.top20Red21Red32Red43.push(item);
        return false;
    }

    //去掉红6-红1小于等于13
    if(number6 - number1 <= 13){
        returnRes.red6red1.push(item);
        return false;
    }

    return true;
  });

  //9.排除近20期的奇偶组合
  let dropTopOddAndEevn = filterLastOddAndEevn(filterResult, historyOddEvenList);
  filterResult = dropTopOddAndEevn.result;//过滤后的结果
  returnRes.dropTopOddAndEevn = dropTopOddAndEevn.removeList;
  
  //10.过滤掉历史已开结果
    filterResult = removeHistoryList(filterResult, historyList).result;
  
    returnRes.filterResult = filterResult;//最终返回的数据
    return returnRes;
}

function removeHistoryList(arr, historyList){
    let removeList = []
    arr = arr.filter(function(item, index){
        let str = item.join("");
        if(historyList[str]){//当前组合已经开过了
            removeList.push(item);
            return false;
        }
        return true;
    });
    return {
        result: arr
    }
}

//排除近20期的奇偶组合
function filterLastOddAndEevn(arr, historyOddEvenList){
    let removeList:any[] = [];
    let topOddAndEven = take(historyOddEvenList, 20);;//top20的奇偶比
    
    arr = arr.filter((item)=>{
        let currentOddEven = getOddAndEven(item);
        let index = topOddAndEven.indexOf(currentOddEven);
        if(index > -1){//当前组合不在近20期内
            removeList.push(item);
            return false
        }
        return true;
    });
    
    return {
        result: arr,
        removeList: removeList
    };
}

export {
    getFilterResult
}